var closeSash = document.getElementsByClassName('close')[0];
var sash = document.getElementsByClassName('sash')[0];
var sashCouponImg = document.getElementsByClassName('paper-img')[0].children
var sashCouponTitle = document.getElementsByClassName('paper-title')[0]
var user = navigator.userAgent;
var android = user.indexOf('Android') > -1 || user.indexOf('Adr') > -1;
var ios = !!user.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
var put_system = 0;
if(ios == true) {
	put_system = 2;
} else if(android == true) {
	put_system = 3;
} else {
	put_system = 4;
}

// 定义优惠券信息
var title = [];
var link = [];
var imgUrl = [];
var couponId = [];
var requestUtl = setting.apiUrl + "/turntable/index";

// 请求成功回调&&优惠券信息收集
//function success(text) {
//	var jsondata = JSON.parse(text);
//	var coupons = jsondata.coupons;
//	for(var i in coupons) {
//		title.push(coupons[i].coupon_title);
//		link.push(coupons[i].coupon_link);
//		imgUrl.push(setting.dataUrl + "/data/upload/thumbs/" + coupons[i].coupon_thumb);
//		couponId.push(coupons[i].coupon_id);
//	}
//}
//请求失败回调
//function fail(code) {
//	console.log(code);
//}

// 数据请求
//var request = new XMLHttpRequest();
var data = {
	'put_system': put_system,
	'activity_id': setting.act_id,
	'latest_time': setting.latest,
	'spm': setting.spm_code
};

function formatParams(data) {
	var arr = [];
	for(var name in data) {
		arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
	}
	return arr.join("&");
}
var str = formatParams(data);
//request.onreadystatechange = function() {
//
//	if(request.readyState === 4) {
//		if(request.status === 200) {
//			return success(request.responseText);
//		} else {
//			return fail(request.status);
//		}
//	} else {}
//}
//request.open('POST', setting.apiUrl + "/turntable/index");
//request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//request.send(str);

// 关闭弹窗
closeSash.addEventListener("click", function() {
	sash.setAttribute("style", "display:none");
})

// 弹窗信息
setTimeout(function() {
	sashCouponImg[0].src = imgUrl[0]
	sashCouponTitle.innerText = title[0]
}, 100)
sashCouponImg[0].addEventListener("click", function() {
	window.location.href = link[0];
})

document.getElementsByClassName('submit')[0].addEventListener("click", function() {
	window.location.href = link[0];
})

//渲染优惠券列表
function list() {
	setTimeout(function() {
		var newCouponId = couponId.slice(1, 7);
		var newTitle = title.slice(1, 7);
		var newLink = link.slice(1, 7);
		var newImgUrl = imgUrl.slice(1, 7);
		for(var i = 0; i < newCouponId.length; i++) {
			var main = document.getElementsByClassName('main')[0];
			var mainCard = document.createElement("div");
			mainCard.setAttribute("class", "main-card");
			mainCard.innerHTML =
				"<a class='card-img'" + " href=" + newLink[i] + ">" +
				"<img src=" + newImgUrl[i] + " alt=" + newCouponId[i] + " />" +
				"</a>" +
				"<div class='card-before'>" +
				"<img src='images/bottom.png' />" +
				"</div>" +
				"<a class='card-button'" + " href=" + newLink[i] + ">" +
				"<img src='images/button.png' / >" +
				"</a>" +
				"<div class='card-title'>" +
				newTitle[i] +
				"</div>"
			main.appendChild(mainCard);
			mainCard.addEventListener('click', function(e) {
				console.log(e);
			})
		}
		ajaxMethod(setting.apiUrl+"/turntable/index", , "POST", function success(data) {
			console.log(data)
		}
	}, 100)

}
list();

function ajaxMethod(url, data, method, success) {
	var ajax = new XMLHttpRequest();
	if(method == "get") {
		if(data) {
			url += '?';
			url += data;
		} else {

		}
		ajax.open(method, url);
		ajax.send();
	} else {
		ajax.open(method, url);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if(data) {
			ajax.send(data);
		} else {
			ajax.send();
		}
	}
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {
			success(ajax.responseText);
		}
	}
}
ajaxMethod(requestUtl, str, "POST", function success(data) {
	var jsondata = JSON.parse(data);
	var coupons = jsondata.coupons;
	for(var i in coupons) {
		title.push(coupons[i].coupon_title);
		link.push(coupons[i].coupon_link);
		imgUrl.push(setting.dataUrl + "/data/upload/thumbs/" + coupons[i].coupon_thumb);
		couponId.push(coupons[i].coupon_id);
	}
})